


module.exports = function (lab) {
    return {
        importBrandSKUFromJSONRPC : function (session,condition = {}, jsonrpc = "jsonrpc") {
            if (!session.currentOU) {
                throw @lab.errors.not_login;
            }
            return lab[jsonrpc].slime_test.doQuery({
                models : "slime_brand_sku_shelves",
                start  : 0,
                limit  : -1
            }).then(function ({list}) {
                @.async.all(list,function (slime_brand_sku_shelve) {
                    lab.find("slime_brand_sku_shelves",slime_brand_sku_shelve.id).then(function (exists_slime_brand_sku_shelve) {
                        if (exists_slime_brand_sku_shelve) {
                            lab.update("slime_brand_sku_shelves",exists_slime_brand_sku_shelve,slime_brand_sku_shelve,{
                                ouid : session.currentOU.id
                            }).pipe(this);
                        } else {
                            lab.create("slime_brand_sku_shelves",@.merge.simple(slime_brand_sku_shelve,{
                                ouid : session.currentOU.id
                            })).pipe(this);
                        }
                    }).pipe(this);
                }).pipe(this);
            }).then(function () {
                lab.slime_test.slime.importBrandSKUFromJSONRPC(session, condition,jsonrpc).pipe(this);
            }).all(function ({remote, local}) {
                lab[jsonrpc].elasticsearch_oms.sku.getBrandSKUExtraInfo(remote.id).then(function (extra_info) {
                    lab.elasticsearch_oms.sku.updateBrandSKUExtraInfo(session, @.merge.simple(extra_info,{
                        id : local.id
                    })).pipe(this);
                }).resolve(local).pipe(this);
            });
        }
    }    
}